package com.mingmen.canting.gongyingshang;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.model.EventBean;
import com.mingmen.canting.model.ZhenBanLeftBean;
import com.mingmen.canting.zhenban.ZhenBanLeftAdapter;
import com.mingmen.canting.zhenban.ZhenBanMySortDetailFragment;
import com.mingmen.canting.zhenbanlibrary.CheckListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

@SuppressLint("ValidFragment")
public class GongYingProductFragment extends BaseFragment implements CheckListener, GongYingLeftAdapter.OnItemClickListener {
    private RecyclerView rvSort;
    private GongYingLeftAdapter mSortAdapter;
     private GongYingProductDetailFragment mSortDetailFragment;
     String getData;
      List<ZhenBanLeftBean.ChildrenBean> childrenBeanList = new ArrayList<>();
    ZhenBanLeftBean zhenBanLeftBean;
    Gson gson;
    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fr_gongyingproduct, null);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        EventBus.getDefault().register(this);
        card_title_tv.setText(getData);
        mContext = getActivity();
        gson = new Gson();
        rvSort = (RecyclerView) v.findViewById(R.id.rv_sort);
        rvSort.setLayoutManager(new LinearLayoutManager(getActivity()));
        Log.d("getDatagetData", getData);
        Log.d("onCreateView", "onCreateView");
        zhenBanLeftBean = new ZhenBanLeftBean();
        zhenBanLeftBean = gson.fromJson(getData, ZhenBanLeftBean.class);
        mSortAdapter = new GongYingLeftAdapter(getActivity(), childrenBeanList);
        rvSort.setAdapter(mSortAdapter);
        childrenBeanList.addAll(zhenBanLeftBean.getChildren());
        Log.d("zhenBanLeftBean", childrenBeanList.size() + "");

        Log.d("childrenBeanList", childrenBeanList.size() + "");
        mSortAdapter.setDataList(childrenBeanList);
        mSortAdapter.setOnItemClickListener(this);


        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        mSortDetailFragment = new GongYingProductDetailFragment();

        Bundle bundle = new Bundle();
        if (childrenBeanList.size()==0){
            bundle.putString("right","0");
        }else {
            bundle.putString("right", childrenBeanList.get(0).getCategoryId());
        }


        mSortDetailFragment.setArguments(bundle);

        fragmentTransaction.add(R.id.lin_fragment, mSortDetailFragment);
        fragmentTransaction.commit();
        return v;
    }

    @Override
    public void initData() {

    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d("onResume", "onResume");
//        zhenBanLeftBean = GsonUtil.GsonToBean(getData, ZhenBanLeftBean.class);

    }


    public static GongYingProductFragment getInstance(String title) {
        GongYingProductFragment sf = new GongYingProductFragment();
        sf.getData = title;
        Log.d("DingDandkdkdkd", title);

        return sf;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    String myreceiveposition;

    private void setChecked(int position, boolean isLeft) {
        Log.d("p-------->", String.valueOf(position));
        mSortAdapter.setCheckedPosition(position);
        myreceiveposition = String.valueOf(position);

    }


    @Override
    public void check(int position, boolean isScroll) {
        setChecked(position, isScroll);
    }


    @Override
    public void onItemClickListener(View view, int pos, List<ZhenBanLeftBean.ChildrenBean> myLiveLists) {
        mSortAdapter.setCheckedPosition(pos);
        Intent intent = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");

//                MyStringUtils.beanToString(dataBeanList.get(0))
        intent.putExtra("pos", myLiveLists.get(pos).getCategoryId());
        getActivity().sendBroadcast(intent);
    }

    public void onEventMainThread(EventBean eventBean) {


    }
}